package com.huxiaohui.ssbx.config;

import com.huxiaohui.ssbx.config.props.WebSocketProperties;
import com.huxiaohui.ssbx.websocket.WebSocketHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 * 使用构造器注入，并从配置绑定允许的跨域源。
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final WebSocketHandler voiceWebSocketHandler;
    private final WebSocketProperties webSocketProperties;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册WebSocket处理器，支持跨域（允许来源来自配置）
        registry.addHandler(voiceWebSocketHandler, "/ws")
                .setAllowedOrigins(webSocketProperties.getAllowedOrigins());
    }
}
